// rdev::Key 枚举名到 JS keyCode 的映射表（常用键，可自行补充）
const rdevKeyToKeyCode = {
    'KeyA': 65, 'KeyB': 66, 'KeyC': 67, 'KeyD': 68, 'KeyE': 69, 'KeyF': 70, 'KeyG': 71, 'KeyH': 72, 'KeyI': 73, 'KeyJ': 74, 'KeyK': 75, 'KeyL': 76, 'KeyM': 77, 'KeyN': 78, 'KeyO': 79, 'KeyP': 80, 'KeyQ': 81, 'KeyR': 82, 'KeyS': 83, 'KeyT': 84, 'KeyU': 85, 'KeyV': 86, 'KeyW': 87, 'KeyX': 88, 'KeyY': 89, 'KeyZ': 90,
    'Num1': 49, 'Num2': 50, 'Num3': 51, 'Num4': 52, 'Num5': 53, 'Num6': 54, 'Num7': 55, 'Num8': 56, 'Num9': 57, 'Num0': 48,
    'Space': 32, 'Return': 13, 'Tab': 9, 'Escape': 27, 'Backspace': 8,
    'UpArrow': 38, 'DownArrow': 40, 'LeftArrow': 37, 'RightArrow': 39,
    'F1': 112, 'F2': 113, 'F3': 114, 'F4': 115, 'F5': 116, 'F6': 117, 'F7': 118, 'F8': 119, 'F9': 120, 'F10': 121, 'F11': 122, 'F12': 123,
    'ShiftLeft': 16, 'ShiftRight': 16, 'ControlLeft': 17, 'ControlRight': 17, 'Alt': 18, 'MetaLeft': 91, 'MetaRight': 92,
    // 可继续补充...
};

import { listen } from "@tauri-apps/api/event";
listen("global_key", e => {
    if (!e.payload) return;
    const keyName = e.payload;
    const keyCode = rdevKeyToKeyCode[keyName];
    if (!keyCode) return;
    // 构造并派发 keydown 事件
    const event = new KeyboardEvent("keydown", {
        key: keyName,
        keyCode: keyCode,
        which: keyCode,
        code: keyName
    });
    document.body.dispatchEvent(event);
});